package com.cwvs.cat.fragment;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.cwvs.cat.Applications;
import com.cwvs.cat.MyFriendSchoolMateInfoActivity;
import com.cwvs.cat.R;
import com.cwvs.cat.adapter.MyActivePublishAdapter;
import com.cwvs.cat.adapter.SchoolMateInviteActiveAdapter;
import com.cwvs.cat.adapter.SchoolMateInviteClubAdapter;
import com.cwvs.cat.bean.ActiveContent;
import com.cwvs.cat.net.Port;

public class SchoolMateInviteActiveFragment extends Fragment{

	private Activity mContext;
	private ListView mListView;
	private SchoolMateInviteActiveAdapter adapter;
	private List<ActiveContent> mList;
	private boolean[] addForInvited;
	private Button sure;
	
	public SchoolMateInviteActiveFragment() {
		// TODO Auto-generated constructor stub
	}

	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = getActivity();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View contentLayout = inflater.inflate(R.layout.fragment_school_mate_invite_active,
				container, false);
		mList = new ArrayList<ActiveContent>();
		
		actMyadd();
		
		initView(contentLayout);
		return contentLayout;
	}

	private void initView(View view) {
		mListView = (ListView) view.findViewById(R.id.lv_hyactiv);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				ImageView img_invite = (ImageView) view.findViewById(R.id.img_invite);
				addForInvited[position] = !addForInvited[position];
				if (addForInvited[position]) {
					img_invite.setImageResource(R.drawable.invite_blue);
				} else {
					img_invite.setImageResource(R.drawable.invite_white);
				}
			}
		});
		sure = (Button) view.findViewById(R.id.sure);
		sure.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if ( !inviteIds().equals("")) {
					yaoqing();
				}
			}
		});
	}
	private String inviteIds() {
		StringBuilder result = null;
		for (int i = 0; i < addForInvited.length; i++) {
			if (addForInvited[i]) {
				if (result != null) {
					result.append(",").append(mList.get(i).id);
				} else {
					result = new StringBuilder(mList.get(i).id);
				}
			}
		}
		System.out.println(result);
		return result.toString();
	}
	private void actMyadd(){
		AjaxParams params = new AjaxParams();
		params.put("token",Applications.user.token);
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.actMyadd,params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}
			@SuppressLint("NewApi")
			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				System.out.println(t.toString());
				try {
					JSONObject obj = null;
					JSONArray array=null,array2=null,arr = null;
					JSONObject json = new JSONObject(t.toString());
					if (json.getInt("code")==0) {
						obj=json.getJSONObject("list");
						
						array2=obj.getJSONArray("header");
						for (int i = 0; i < array2.length(); i++) {
							String cate2 = (String) array2.get(i);
						//	list2.add(cate2 );
						}
						mList.clear();
						array=obj.getJSONArray("data");
						for (int i = 0; i < array.length(); i++) {
							arr = (JSONArray) array.get(i);
							List<String>	list1 = new ArrayList<String>();
							for (int k = 0; k < arr.length(); k++) {
								String cate1 =(String) arr.get(k);
								list1.add(cate1);
							}
							ActiveContent msgSystem = new ActiveContent(list1);
							mList.add(msgSystem);
							
						}
						addForInvited = new boolean[mList.size()];
						for (int i = 0; i < addForInvited.length; i++) {
							addForInvited[i] = false;
						}
						adapter = new SchoolMateInviteActiveAdapter(getActivity(), mList);
						mListView.setAdapter(adapter);
					}else {
						Toast.makeText(getActivity(), json.getString("msg"), 2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	private void yaoqing(){
		AjaxParams params = new AjaxParams();
		params.put("token",Applications.user.token);
		params.put("act_id",inviteIds());
		params.put("uid",MyFriendSchoolMateInfoActivity.userId);
		System.out.println("params==="+params.toString());
		FinalHttp fh = new FinalHttp();
		fh.post(Port.invitemember,params, new AjaxCallBack<Object>() {
			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
			}
			@SuppressLint("NewApi")
			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				System.out.println(t.toString());
				try {
					JSONObject json = new JSONObject(t.toString());
					if (json.getInt("code")==0) {
						Toast.makeText(getActivity(), json.getString("msg"), 2000).show();
					}else {
						Toast.makeText(getActivity(), json.getString("msg"), 2000).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	

}